/*
 * @Author: 云风 1558003416@qq.com
 * @Date: 2022-05-11 11:48:02
 * @LastEditors: 云风 1558003416@qq.com
 * @LastEditTime: 2022-05-13 17:52:32
 * @FilePath: \vue3_admin_jinxu\src\router\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createRouter, createWebHashHistory } from "vue-router";

const routes = [
  {
    path: "/",
    name: "Layout",
    redirect: "/home",
    meta: {
      requied: true,
      index: 1,
    },
    component: () =>
      import(/* webpackChunkName: "about" */ "@/components/layout"),
    children: [
      {
        path: "home",
        name: "首页",
        component: () => import("../views/home"),
        meta: {
          requied: true,
          index: 2,
        },
      },
      {
        path: "color",
        name: "主题管理",
        component: () => import("../views/color"),
        meta: {
          requied: true,
          index: 3,
        },
      },
      {
        path: "global",
        name: "国际化",
        component: () => import("../views/global"),
        meta: {
          requied: true,
          index: 4,
        },
      },
      {
        path: "echarts",
        name: "echarts的使用",
        component: () => import("../views/dynamicchart"),
        meta: {
          requied: true,
          index: 5,
        },
      },
      {
        path: "area",
        name: "拖拽 的使用",
        component: () => import("../views/area"),
        meta: {
          requied: true,
          index: 6,
        },
      },
      {
        path: "user",
        name: "角色管理",
        component: () => import("../views/user"),
        meta: {
          requied: true,
          index: 7,
        },
      },
      {
        path: "role",
        name: "菜单管理",
        component: () => import("../views/role"),
        meta: {
          requied: true,
          index: 7,
        },
      },
      {
        path: "echart",
        name: "图表",
        component: () => import("../views/echarts"),
        meta: {
          requied: true,
          index: 7,
        },
      },
      {
        path: "comun",
        name: "组件通信",
        component: () => import("../views/comun"),
        meta: {
          requied: true,
          index: 9,
        },
      },
    ],
  },
  {
    path: "/login",
    name: "Login",
    meta: {},
    component: () => import(/* webpackChunkName: "login" */ "../views/login"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  console.log(to, from);

  if (to.path == "/login") {
    next();
    return;
  }

  const tokenTemp = localStorage.getItem("usrToken");

  if (tokenTemp) {
    if (to.meta.require) {
      next();
    } else {
      next();
    }
  } else {
    next("/login");
  }
  // try {
  //   await askForCameraPermission()
  // } catch (error) {
  //   if (error instanceof NotAllowedError) {
  //     // ... 处理错误，然后取消导航
  //     return false
  //   } else {
  //     // 意料之外的错误，取消导航并把错误传给全局处理器
  //     throw error
  //   }
  // }
});

export default router;
